package com.avic.rong.questionOrAnswer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

public class UMShareUtil extends UmDialog {

	private ShareUtilCallBack shareUtilCallBack;
	private Activity mAct;
	private String contentUrl;// 分享链接
	private String title="";// 分享title
	private String content="";// 分享内容
	private String shareImgUrl;// 分享图片url

	private ProgressDialog progressDialog;
	private SharedPreferencesUtil sharedPreferencesUtil;

	public UMShareUtil(ShareUtilCallBack shareUtilCallBack, Activity mAct, String contentUrl, final String title, final String content, String shareImgUrl) {
		this.shareUtilCallBack = shareUtilCallBack;
		this.mAct = mAct;
		this.contentUrl = contentUrl;
		this.title = title;
		if (StringUtil.isEmpty(content)){
			this.content = title;
		}else{
			this.content = content;
		}
		this.shareImgUrl = shareImgUrl;
		/*if (shareImgUrl != null) {
			this.shareImgUrl = shareImgUrl.replace("0_0", "200_200");
		}*/
	}

	// share dialog 点击回调(点击了哪个第三方分享)
	public void shareDialogClick(SHARE_MEDIA mShareMedia) {
		startShare(mShareMedia);
	}

	public void startShare(SHARE_MEDIA shareMedia) {
		if (!StringUtil.isEmpty(shareImgUrl)){
			UMImage uImg = new UMImage(mAct, shareImgUrl);
			startShare(shareMedia, uImg); // TRUE 弹出toast
		}else{
			UMImage uImg = new UMImage(mAct,R.drawable.ic_default_logo);
			startShare(shareMedia, uImg); // TRUE 弹出toast
		}


	}

	public void startShare(SHARE_MEDIA shareMedia, UMImage uImg) {
		ShareAction shaAction = new ShareAction(mAct);

		if (shareMedia == SHARE_MEDIA.QQ) {
			UMWeb umweb = new UMWeb(contentUrl);
			umweb.setTitle(title);
			umweb.setDescription(content);
			if (uImg!=null){
				umweb.setThumb(uImg);
			}
			shaAction.setPlatform(SHARE_MEDIA.QQ).withMedia(umweb).setCallback(shareListener).share();
			hideProgressDialog();
		} else if (shareMedia == SHARE_MEDIA.QZONE) {
			UMWeb umweb = new UMWeb(contentUrl);
			umweb.setTitle(title);
			umweb.setDescription(content);
			if (uImg!=null){
				umweb.setThumb(uImg);
			}
			shaAction.setPlatform(SHARE_MEDIA.QZONE).withMedia(umweb).setCallback(shareListener).share();
		} else if (shareMedia == SHARE_MEDIA.WEIXIN) {
			if (isWXAppInstalledAndSupported(mAct)) {
				UMWeb umweb = new UMWeb(contentUrl);
				umweb.setTitle(title);
				umweb.setDescription(content);
				if (uImg!=null){
					umweb.setThumb(uImg);
				}
				shaAction.setPlatform(SHARE_MEDIA.WEIXIN).withMedia(umweb).setCallback(shareListener).share();
			} else {
				if (shareUtilCallBack != null) {
					shareUtilCallBack.shareFailed();
				}
			}
		} else if (shareMedia == SHARE_MEDIA.WEIXIN_CIRCLE) {
			if (isWXAppInstalledAndSupported(mAct)) {
				UMWeb umweb = new UMWeb(contentUrl );
				umweb.setTitle(title);
				umweb.setDescription(content);
				if (uImg!=null){
					umweb.setThumb(uImg);
				}
				shaAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).withMedia(umweb).setCallback(shareListener).share();
			} else {
				if (shareUtilCallBack != null) {
					shareUtilCallBack.shareFailed();
				}
			}
		} else if(shareMedia== SHARE_MEDIA.SINA){
			if (!TextUtils.isEmpty(shareImgUrl)){
				shaAction.setPlatform(SHARE_MEDIA.SINA).withText(title+"\n"+content+contentUrl).withMedia(uImg).setCallback(shareListener).share();
			}else{
				shaAction.setPlatform(SHARE_MEDIA.SINA).withText(title+"\n"+content+contentUrl).setCallback(shareListener).share();
			}


		}
	}

	UMShareListener shareListener = new UMShareListener() {

		@Override
		public void onCancel(SHARE_MEDIA arg0) {
			Log.i("分享=====","分享取消");
			ToastTools.showToast(mAct, "分享取消");
			hideProgressDialog();
			if (shareUtilCallBack != null) {
				shareUtilCallBack.shareCancel();
			}

		}

		@Override
		public void onError(SHARE_MEDIA arg0, Throwable arg1) {
			Log.i("分享=====","分享失败");
			ToastTools.showToast(mAct, "分享失败");
			hideProgressDialog();
			if (shareUtilCallBack != null) {
				shareUtilCallBack.shareFailed();
			}

		}

		@Override
		public void onStart(SHARE_MEDIA share_media) {
			showProgressDialog(mAct, R.string.progress_dialog_tip_type3);
		}

		@Override
		public void onResult(SHARE_MEDIA arg0) {
			Log.i("分享=====","分享成功");
			ToastTools.showToast(mAct, "分享成功");
			hideProgressDialog();
			if (shareUtilCallBack != null) {
				shareUtilCallBack.shareSuccess();
			}

		}
	};



	// 此方法需要在分享的activity的onActivityResult方法中调用
	public void initActivityResultSsoHandler(int requestCode, int resultCode, Intent data) {
		/** attention to this below ,must add this **/
		UMShareAPI.get(mAct).onActivityResult(requestCode, resultCode, data);
	}

	public static boolean isWXAppInstalledAndSupported(Context context) {
		IWXAPI wxapi = WXAPIFactory.createWXAPI(context, MyApplication.AppID);
		boolean sIsWXAppInstalledAndSupported = wxapi.isWXAppInstalled();
		if (!sIsWXAppInstalledAndSupported) {
			ToastTools.showToast(context, "您未安装微信或版本不支持");
		}

		return sIsWXAppInstalledAndSupported;
	}

	public void onPause() {
		hideProgressDialog();
	}



}
